package com.imay.live.helper;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.util.FileUtil;
import com.imay.live.IMApplication;
import com.imay.live.model.GiftModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by jeaman on 2017/4/20.
 */

public class GiftHelper {
    private final static int REQUEST_STATE_IDE = 0;
    private final static int REQUEST_STATE_ING = 1;
    private final static int REQUEST_STATE_SUCCESS = 2;
    private final static int REQUEST_STATE_ERROR = 3;

    private static GiftHelper instance;

    private File mCacheFile;
    private Map<String,List<GiftModel>> mGiftList;
    private int mRequestState;

    private GiftHelper() {
        mCacheFile = new File(IMApplication.getInstance().getCacheDir(), "gift_list.json");
        loadGift();
    }

    public synchronized static GiftHelper getInstance() {
        if (instance == null) {
            instance = new GiftHelper();
        }
        return instance;
    }

    private void loadGift() {
        String data = FileUtil.getString(mCacheFile);
        if (data != null) {
            Result<Map<String,List<GiftModel>>> result = JSON.parseObject(data, new TypeReference<Result<Map<String,List<GiftModel>>>>() {}.getType());
            if (result != null && result.data != null) {
                mGiftList = result.data;
            }
        }
    }

    public String getGiftName(int giftId) {
        GiftModel gift = getGift(giftId);
        return gift == null ? IMApplication.getInstance().getString(R.string.gift_unknown_name) : gift.getGiftName();
    }

    public GiftModel getGift(int giftId) {
        if (mRequestState == REQUEST_STATE_IDE || mRequestState == REQUEST_STATE_ERROR) {
            requestGift();
        }
        if (mGiftList != null) {
            GiftModel gift = null;
            for (List<GiftModel> models : mGiftList.values()) {
                for (int i = 0; i < models.size(); i++) {
                    gift = models.get(i);
                    if (gift.getGiftId() == giftId) {
                        return gift;
                    }
                }
            }
        }

        return null;
    }

    public void requestGift() {
        mRequestState = REQUEST_STATE_ING;
        ProtoRequestHelper.task(ProtoHttpFactory.requestGiftList(0,0)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                Result<Map<String,List<GiftModel>>> result = JSON.parseObject(data, new TypeReference< Result<Map<String,List<GiftModel>>>>() {}.getType());
                if (result.result == ResultCode.CodeSuccess && result.data != null) {
                    mGiftList = result.data;
                    mRequestState = REQUEST_STATE_SUCCESS;
                    EventHelper.postGiftLoaded();
                    FileUtil.writeString(mCacheFile, data);
                } else {
                    mRequestState = REQUEST_STATE_ERROR;
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                mRequestState = REQUEST_STATE_ERROR;
            }
        });
    }

}
